package action;

import Impl.TopicDaoImpl;
import Impl.plateDaoImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(name = "DeleteArticle",urlPatterns = "/deletearticle.jhtml")
public class DeleteArticle extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String sid=request.getParameter("sid");
        int tid=Integer.parseInt(request.getParameter("tid"));
        String sname=request.getParameter("sname");
        TopicDaoImpl topicDao=new TopicDaoImpl();
        topicDao.deleteTopicById(tid);
        System.out.println(tid);
        String url="show.jhtml?sname="+sname+"&sid="+sid;
        System.out.println(url);
        new TopicDaoImpl().getTopicCountById(Integer.parseInt(sid));
        int sid1 = new plateDaoImpl().findPlateBySid(Integer.parseInt(sid)).get(0).getsParentId();
        if (sid1==0){
            new plateDaoImpl().updateTopicCounts(Integer.parseInt(sid));
        }
        new plateDaoImpl().updateTopicCounts(sid1);
        request.getRequestDispatcher(url).forward(request,response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }
}
